package com.usefullc.system.controller.admin;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.YesNoEnum;
import com.usefullc.common.user.OnlineUser;
import com.usefullc.common.user.OnlineUserManager;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.domain.JobTemplate;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.param.JobTemplateEnabledParam;
import com.usefullc.system.param.JobTemplateParam;
import com.usefullc.system.param.JobTemplateStartNowParam;
import com.usefullc.system.query.JobTemplateQuery;
import com.usefullc.system.service.JobTemplateService;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.system.vo.JobTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 定时任务模板控制类
 * @author: 星空
 * @date: 2022-09-06
 */
@RestController
@RequestMapping("/admin/jobTemplate/")
@Api( tags="定时任务模板")
public class AdminJobTemplateController extends BaseController {

    @Autowired
    private JobTemplateService jobTemplateService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "定时任务模板列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) JobTemplateQuery query) {
        Pager<JobTemplateVo> page = jobTemplateService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "定时任务模板所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) JobTemplateQuery query) {
        List<JobTemplate> dataList = jobTemplateService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "定时任务模板详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        JobTemplate JobTemplate = jobTemplateService.findById(id);
        return WebResultBuilder.successResult(JobTemplate);
    }



    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建定时任务模板", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody JobTemplateParam param) {
        OnlineUser loginUser = OnlineUserManager.getLoginUser();
        param.setTenantId(loginUser.getTenantId());
        JobTemplate jobTemplate = BeanUtils.beanCopy(param, JobTemplate.class);
        jobTemplateService.saveJob(jobTemplate);
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改定时任务模板", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody JobTemplateParam param) {
        JobTemplate oldBean = jobTemplateService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        jobTemplateService.updateJob(oldBean);
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除定时任务模板",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        jobTemplateService.deleteJob(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除定时任务模板",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        jobTemplateService.deleteJob(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改是否可用", response = WebResult.class)
    @PostMapping(path = "/updateEnabled", produces = "application/json")
    public WebResult updateEnabled(@UsefulValid(update=true) @RequestBody JobTemplateEnabledParam param) {
        if(param.getEnabled() == YesNoEnum.YES.getCode()){
            jobTemplateService.enableJob(param.getIds());
        }else{
            jobTemplateService.disableJob(param.getIds());
        }
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "立即执行", response = WebResult.class)
    @PostMapping(path = "/startNow", produces = "application/json")
    public WebResult startNow(@UsefulValid(update=true) @RequestBody JobTemplateStartNowParam param) {
        jobTemplateService.startNow(param.getId(),param.getParam());
        return successResult();
    }





}
